<?php


namespace app\api\controller;


use app\common\controller\Api;
use app\common\model\UserIntention;
use app\common\model\StaffIntention;
use Exception;
use think\Db;

/**
 * 意向接口
 */
class Intention extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\UserIntention();
    }
    //意向
    public function list()
    {
        $where = ['state' => 1];
        $this->comonlist($where);
        $this->success('成功');
    }
    //我的意向
    public function userlist()
    {
        $where = ['user_id' => $this->auth->id];
        $this->comonlist($where);
        $this->success('成功');
    }

    protected function comonlist($where = [])
    {
        $page = $this->request->get('page');
        $limit = $this->request->get('limit',LIMIT);

        $list =  $this->model->with('user')->where($where)->order('id desc')->paginate($limit,false,['page' => $page]);

        $result = array("total" => $list->total(), "rows" => $list->items());
        $this->success('返回成功',$result);
    }
    //意向详情
    public function detail()
    {
        $id = $this->request->get('id');
        $info = $this->model->where(['id' => $id])->find();

        //报名人数
        $model = new StaffIntention();
        $staff = null;
        $staff_data = [];
        if(!empty($info->staff_id)) {
            $staff = \app\common\model\Staff::get($info->staff_id);
            $staff->hidden(['mobile', 'address', 'updatetime','deletetime']);
        }else {
            $staff_data = $model->with('staff')->where(['user_intention_id' => $id])->select();
        }

        foreach ($staff_data as $row) {
            $row->getRelation('staff')->hidden(['mobile', 'address', 'updatetime','deletetime']);
        }
        $this->success('成功',[
            'info' => $info,
            'staff_data' => $staff_data,
            'staff' => $staff
        ]);
    }
    //确定意向
    public function agree()
    {

        $id = $this->request->get('id');
        $staff_id = $this->request->get('staff_id');
        $info = $this->model->where(['id' => $id])->find();

        if(empty($info)) $this->error('参数错误');
        if(!empty($info->staff_id)) $this->error('已选择技师');

        //消息
        if ( $info->save([
            'staff_id' => $staff_id,
            'state' => -1
        ])) {
            $this->success('确定成功');
        } else {
            $this->error('确定失败');
        }
    }
    //报名
    public function apply()
    {
        $id = $this->request->get('id');
        $staff = \app\common\model\Staff::get(['user_id' => $this->auth->id]);
        $staff1 = StaffIntention::get(['staff_id' => $staff->id]);
        if($staff1) $this->error('已经报名');
        $ret =StaffIntention::create([
            'user_intention_id' => $id,
            'staff_id' => $staff->id,
        ]);
        //消息
        if ($ret) {
            $this->success('报名成功');
        } else {
            $this->error('报名失败');
        }
    }

    //提交意愿
    public function submit()
    {
        $data = $this->request->post();
        Db::startTrans();
        try {
            $data['user_id'] = $this->auth->id;
            $data['starttime'] = strtotime($data['starttime']);
            $data['endtime'] = strtotime($data['endtime']);
            UserIntention::create($data);
            Db::commit();
        }catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success('提交成功');
    }
}